#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <vector>

/**
 * Luogu P1140: Gene Similarity
 */

std::unordered_map<char, int> charToBase{
    {'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}};

const int BLANK = 4;

int sim[5][5]{
    {5, -1, -2, -1, -3}, {-1, 5, -3, -2, -4}, {-2, -3, 5, -2, -2},
    {-1, -2, -2, 5, -1}, {-3, -4, -2, -1, 0},
};

int lenA, lenB;
int A[100], B[100];
int buf[101][101];

void read(int len, int *arr) {
    for (int i = 0; i < len; i++) {
        char c;
        scanf("%c", &c);
        arr[i] = charToBase[c];
    }
}

int main() {
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    // Input
    scanf("%d ", &lenA);
    read(lenA, A);
    scanf("\n%d ", &lenB);
    read(lenB, B);

    // Use DP to solve
    buf[0][0] = 0;
    for (int i = 1; i <= lenA; i++)
        buf[i][0] = buf[i - 1][0] + sim[A[i - 1]][BLANK];
    for (int i = 1; i <= lenB; i++)
        buf[0][i] = buf[0][i - 1] + sim[BLANK][B[i - 1]];
    for (int i = 1; i <= lenA; i++) {
        int a = A[i - 1];
        for (int j = 1; j <= lenB; j++) {
            int b = B[j - 1];
            if (a == b)
                buf[i][j] = buf[i - 1][j - 1] + sim[a][b];
            else {
                int noBlank = buf[i - 1][j - 1] + sim[a][b];
                int leftBlank = buf[i][j - 1] + sim[BLANK][b];
                int rightBlank = buf[i - 1][j] + sim[a][BLANK];
                buf[i][j] = std::max({noBlank, leftBlank, rightBlank});
            }
        }
    }
    printf("%d\n", buf[lenA][lenB]);

    return 0;
}
